<template>
  <div class="page jisuyun">
    <StoreChoice ref="StoreChoice"
                 :multipleChoice="false"
                 :current="store"
                 @listenToStoreSelect="getSelectStoreValue"
                 :statusFilter="false"
                 :hasTotal="false"
                 :hideTitle="true"
                  />
    <div class="scroll-view">
      <customHeader style="display:none"
                    middleTitle="极速云" />
      <div class="page-body">
        <div class="authorize-info"
             v-if="needAuth">
          <p class="info-title">授权获取以下信息</p>
          <p class="info-item"><em></em>商户信息（商户名称、法人名称、注册日期、行业等）</p>
          <p class="info-item"><em></em>交易流水（近6个月、24个月交易汇总）</p>
          <p class="more-info">点击确认即您同意第三方介入我司平台，并且有权限从我司平台拉取相应权限的数据信息</p>
          <div class="btn-wrap">
            <div class="btn"
                 @click="doJisuyunAuth">确认授权</div>
          </div>
        </div>
        <div class="form-wrapper new-form-list"
             v-if="!needAuth&&needSelectShop">
          <div class="common-notice blue">
            <i class="zhk-ts"></i>请选择要贷款的门店, 我们会将门店数据加密后回传给第三方
          </div>
          <div class="form-item border-bt"
               @click="handleClickSelectStore">
            <div class="label">门店</div>
            <div class="pick-value"
                 v-if="store.length !== 0">{{formateStore}}</div>
            <div class="pick-value"
                 v-if="store.length == 0">请选择门店</div>
            <i class="zhk-list_rarrow"></i>
          </div>
          <div class="btn-wrap">
            <div v-if="store.length ==0"
                 class="btn disabled">确认</div>
            <div v-else
                 class="btn"
                 @click="doSelectJisuyunShopSelect">确认</div>
          </div>
        </div>

      </div>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import customHeader from "../../components/header/header";
import { Toast } from "mint-ui";
import StoreChoice from "../../components/StoreChoice/StoreChoice";
import jsBridge from "../../tools/jsBridge.js";
export default {
  name: "jisuyun",
  data() {
    return {
      data: "",
      roleInfo: {},
      isAllStore: false,
      store: [],
      needAuth: false,
      needSelectShop: false
    };
  },
  computed: {
    formateStore() {
      let val = "";
      if (this.roleInfo.role == "4") {
        this.store = [
          {
            selectName: this.roleInfo.shopName,
            selectKey: this.roleInfo.yzShopId
          }
        ];
        val = this.roleInfo.shopName;
      } else if (!this.isAllStore) {
        if (this.store.length === 1) {
          val = this.store[0].selectName;
        } else {
          val = "已选择" + this.store.length + "家门店";
        }
      } else {
        val = "全部门店";
      }
      return val;
    }
  },
  methods: {
    title(showClose) {
      let temp = "极速云";
      if (this.needAuth) {
        temp = "服务授权";
      } else {
        if (this.needSelectShop) {
          temp = "选择门店";
        } else {
          temp = "极速云";
        }
      }
      jsBridge.setTitle(temp,showClose);
    },
    checkJumpToJisuyun() {
      this.title();
      if (!this.needAuth) {
        if (!this.needSelectShop) {
          if (this.directUrl && this.directUrl !== "") {
            this.title(true);
            window.selfControlBack = false;
            window.AppUtil.call(window.AppUtil.APIS.SET_BACK_OPTION, {
              control: false,
              type:'history'
            });
            setTimeout(() => {
              this.title(true);
              window.AppUtil.call(window.AppUtil.APIS.SET_BACK_OPTION, {
                control: false,
                type:'history'
              });
              window.location.replace(this.directUrl);
            }, 200);
          }
        }
      }
    },
    checkJisuyunAuthorize() {
      this.ajax({
        url: "/newapp/ads/jisuyunAuthQuery.do",
        type: "get",
        controlLoading: true
      })
        .then(res => {
          if (res.success) {
            if (res.resultObject) {
              this.needAuth = res.resultObject.needAuth;
              //this.needAuth = true;
              this.needSelectShop = res.resultObject.needSelectShop;
              this.directUrl = res.resultObject.directUrl;
              this.checkJumpToJisuyun();
            } else {
              Toast(res.msg);
            }
          } else {
            Toast(res.msg);
          }
        })
        .catch(err => {
          Toast(err.msg);

        });
    },
    doJisuyunAuth() {
      this.ajax({
        url: "/newapp/ads/jisuyunAuth.do",
        type: "get",
        controlLoading: true
      })
        .then(res => {
          if (res.success) {
            if (res.resultObject) {
              this.needAuth = !res.resultObject.authResult;
              this.needSelectShop = res.resultObject.needSelectShop;
              this.directUrl = res.resultObject.directUrl;
              this.checkJumpToJisuyun();
            } else {
              Toast(res.msg);
            }
          } else {
            Toast(res.msg);
          }
        })
        .catch(err => {
          Toast(err.msg);
        });
    },
    doSelectJisuyunShopSelect() {
      this.ajax({
        url: "/newapp/ads/jisuyunShopSelect.do",
        type: "post",
        data: { shopId: this.store[0].selectKey },
        controlLoading: true
      })
        .then(res => {
          if (res.success) {
            if (res.resultObject) {
              this.directUrl = res.resultObject;
              this.needSelectShop = false;
              this.checkJumpToJisuyun();
            } else {
              Toast(res.msg);
            }
          } else {
            Toast(res.msg);
          }
        })
        .catch(err => {
          Toast(err.msg);
        });
    },
    //打开门店选择
    handleClickSelectStore() {
      if (this.roleInfo.manyShopFlag!==undefined&&!this.roleInfo.manyShopFlag) {
        return;
      }
      this.$refs.StoreChoice.popupShow();
    },
    getSelectStoreValue(value, storeName) {
      if (storeName === "全部门店") {
        this.isAllStore = true;
      } else {
        this.isAllStore = false;
      }
      this.store = value;
    },
    submit() {}
  },
  components: {
    customHeader,
    StoreChoice
  },
  mounted() {
    let param = this.$route.query;
    CommonUtil.getUserInfo().then(data => {
      this.roleInfo = data;
      if (this.roleInfo.manyShopFlag!==undefined&&!this.roleInfo.manyShopFlag) {
        this.store = [
          {
            selectName: this.roleInfo.shopName,
            selectKey: this.roleInfo.yzShopId
          }
        ];
      }
      this.checkJisuyunAuthorize();
    });
  }
};
</script>
<style lang='scss' scoped>
@import "jisuyun";
</style>
